﻿using System;
using System.Windows.Media.Imaging;
using SomethingSpacial.CustomControls;
using SomethingSpacial.Model;
using SomethingSpacialClassLib.Web;
using System.Collections.ObjectModel;
using System.ServiceModel.DomainServices.Client;

namespace SomethingSpacial.ViewModel
{
    public class PastEventsViewModel : ViewModelBase
    {
        private EventModel MyModel = new EventModel();
        private SomethingSpacialContext domainContext;
        private SomethingSpacialContext DomainContext
        {
            get
            {
                if (domainContext == null)
                    domainContext = new SomethingSpacialContext();
                return domainContext;
            }
        }
        LoadOperation LoadEvents;

        public PastEventsViewModel()
        {
            Events = new ObservableCollection<EventModel>();
        }

        public ObservableCollection<EventModel> Events
        {
            get;
            private set;
        }

        #region "ViewModel Methods"

        public void loadEvents()
        {
            //Question.  How do we switch between the dynamic and static versions?  Yes, I know my version is
            //                 a lame workaround (ds 12/27/09)
            Boolean boolShowDynamic = true;
            if (boolShowDynamic == false)
            {
                Events.Add(new EventModel());
            }
            else
            {
                LoadEvents = DomainContext.Load(DomainContext.GetEventsPreviousQuery());
                LoadEvents.Completed += new EventHandler(LoadEvents_Completed);
            }
        }

        private void LoadEvents_Completed(object sender, EventArgs e)
        {
            //Not sure why this is required.  Wouldn't it have to be "Complete" in order for this event to be triggered? (ds 09/26/09)
            if (LoadEvents.IsComplete == true)
            {
                foreach (Entity entity in LoadEvents.Entities)
                {
                    Event evt = entity as Event;

                    var userGroupEvent = new EventModel();

                    userGroupEvent.EventID = evt.EVT_Id;
                    userGroupEvent.TopicDescription = evt.EVT_Description;
                    userGroupEvent.TopicTitle = evt.EVT_Name;
                    //userGroupEvent.Location = evt.EVT_Location;
                    userGroupEvent.TopicDescription = evt.EVT_Description;
                    //userGroupEvent.Registration = evt.EVT_RegistrationUrl;
                    userGroupEvent.When = evt.EVT_Start_Date.ToShortDateString() + " " + evt.EVT_Start_Date.ToShortTimeString();
                    Events.Add(userGroupEvent);

                    

                }

            }

        }

        #endregion



        public BitmapImage LogoImage
        {
            get 
            { 
                BitmapImage TempImage = new BitmapImage();
                TempImage.UriSource = new Uri(MyModel.LogoImage, UriKind.RelativeOrAbsolute);
                return TempImage; 
            } 
        }

        public string TopicTitle
        {
            get { return MyModel.TopicTitle; }
            set
            {
                if (MyModel.TopicTitle != value)
                {
                    MyModel.TopicTitle = value;
                    InvokePropertyChanged("TopicTitle");
                }
            }
        }

        public string TopicDescription
        {
            get { return MyModel.TopicDescription; }
            set
            {
                if (MyModel.TopicDescription != value)
                {
                    MyModel.TopicDescription = value;
                    InvokePropertyChanged("TopicDescription");
                }
            }
        }

        public static string TwitterUrl
        {
            get { return EventModel.TwitterUrl; }
        }

        public static string RssUrl
        {
            get { return EventModel.RssUrl; }
        }

        public static string RegistrationURL
        {
            get { return EventModel.RegistrationURL; }
        }

        public static string FcbkUrl
        {
            get { return EventModel.FcbkUrl; }
        }

        public string When
        {
            get { return MyModel.When; }
            set
            {
                if (MyModel.When != value)
                {
                    MyModel.When = value;
                    InvokePropertyChanged("When");
                }
            }
        }

        public string AddressLine1
        {
            get { return MyModel.AddressLine1; }
            set
            {
                if (MyModel.AddressLine1 != value)
                {
                    MyModel.AddressLine1 = value;
                    InvokePropertyChanged("AddressLine1");
                }
            }
        }

        public string AddressLine2
        {
            get { return MyModel.AddressLine2; }
            set
            {
                if (MyModel.AddressLine2 != value)
                {
                    MyModel.AddressLine2 = value;
                    InvokePropertyChanged("AddressLine2");
                }
            }
        }

        public static string MeetingInvite
        {
            get { return EventModel.MeetingInvite; }
        }
    }
}
